const cloudHelper = require("../../helper/cloud_helper.js");
const util = require('../../utils/util.js');

Page({
    data: {
        bannerList: [
            '../../images/icons/default-bg.png',
        ],
        formData: {
            startPlace: '',
            endPlace: '',
        },
        user_info: {
            open_id: '',
            avatar: '',
            nickname: '',
            phone: '',
            sex: '',
            is_admin: '',
        },
    },

    onLoad(option) {
        this.getBanner();
        this.getUserInfo();
        if (option && option['spread_openid']) {
            wx.setStorageSync("spread_openid", option['spread_openid']);
        }
        if (option && option['startPlace']) {
            this.setData({
                'formData.startPlace': option['startPlace']
            });
        }
        if (option && option['endPlace']) {
            this.setData({
                'formData.endPlace': option['endPlace']
            });
        }
        // 入口来自朋友圈
        if (option && option['from'] && option['from'] == 'moments') {
            // wx.showToast({
            //     title: '来自朋友圈的分享',
            // })
        }
    },

    onPullDownRefresh() {
        this.getBanner();
        wx.stopPullDownRefresh();
    },

    // 分享
    onShareAppMessage: function () {
        return {
            title: '',
            path: 'pages/index/index?spread_openid=' + this.data.user_info.open_id + '&startPlace=' + this.data.formData.startPlace + '&endPlace=' + this.data.formData.endPlace
        }
    },

    // 分享朋友圈配置
    onShareTimeline: function () {
        // 记录分享朋友圈的时间
        cloudHelper.callCloudSumbitAsync('api/user/record_share_moments_time');
        // 返回分享朋友圈的配置
        return {
            title: '',
            query: 'from=moments&spread_openid=' + this.data.user_info.open_id + '&startPlace=' + this.data.formData.startPlace + '&endPlace=' + this.data.formData.endPlace,
            imageUrl: this.data.bannerList[0],
        }
    },

    // 获取用户信息
    async getUserInfo() {
        let data = await cloudHelper.callCloudSumbitAsync('api/user/get_info');
        if (data && data.data && data.code == 200) {
            let userInfo = data.data;
            wx.setStorageSync("user_info", userInfo);
            this.setData({
                user_info: {
                    ...this.data.user_info,
                    open_id: userInfo.open_id,
                    nickname: userInfo.nickname,
                    avatar: userInfo.avatar,
                    phone: util.encryptPhone(userInfo.phone),
                    sex: userInfo.sex,
                    is_admin: userInfo.is_admin
                }
            });
        }
    },

    async getBanner() {
        const config = await cloudHelper.callCloudSumbitAsync('api/home/get_config', {
            keys: ['home_banner', 'home_start_place', 'home_end_place']
        });
        if (config && config.data && config.data.list) {
            if (config.data.list.home_banner) {
                this.setData({
                    bannerList: [config.data.list.home_banner.value]
                })
            }
            if (config.data.list.home_start_place) {
                if (this.data.formData.startPlace == '' && this.data.formData.endPlace == '') {
                    this.setData({
                        'formData.startPlace': config.data.list.home_start_place.value
                    })
                }
            }
            if (config.data.list.home_end_place) {
                if (this.data.formData.startPlace == '' && this.data.formData.endPlace == '') {
                    this.setData({
                        'formData.endPlace': config.data.list.home_end_place.value
                    })
                }
            }
        }
    },

    // 选择发车地或目的地
    onSelectCity(e) {
        let type = e.currentTarget.dataset['type'];
        wx.navigateTo({
            url: '/pages/city/index?type=' + type,
        });
    },

    // 切换城市
    onSwitch() {
        let formData = JSON.parse(JSON.stringify(this.data.formData));
        formData.startPlace = this.data.formData.endPlace;
        formData.endPlace = this.data.formData.startPlace;
        this.setData({
            formData
        });
    },

    // 点击热门路线标签
    onHotTag(e) {
        let startPlace = e.currentTarget.dataset.startPlace;
        let endPlace = e.currentTarget.dataset.endPlace;
        this.setData({
            'formData.startPlace': startPlace,
            'formData.endPlace': endPlace,
        });
        this.onSelect();
    },

    // 查询
    onSelect() {
        let formData = this.data.formData;
        if (formData.startPlace == '') {
            wx.showToast({
                mask: true,
                title: '发车地不能为空',
                icon: 'error'
            })
            return;
        }
        if (formData.endPlace == '') {
            wx.showToast({
                mask: true,
                title: '目的地不能为空',
                icon: 'error'
            })
            return;
        }
        wx.navigateTo({
            url: '/pages/calendar/index?startPlace=' + formData.startPlace + '&endPlace=' + formData.endPlace,
        })
    }
});